﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PServiceBus.Core.Interface;
using PServiceBus.Core.Security;
using System.Diagnostics;
using PServiceBus.Gateway.Interfaces.Commands;
using PServiceBus.Gateway.Interfaces.DTO;
using PServiceBus.Gateway.Interfaces;
using Phoenix.RedisConnectors;

namespace PServiceBus.Gateway.Redis.Commands {
    public sealed class RedisBusMessage : BusMessage, IRedisCommand {
        #region IRedisCommand Members

        public bool IsAsync { get; set; }

        public string Key { get; set; }

        public RedisPatternType Pattern { get; set; }

        public string CommandID { get; set; }

        #endregion
        public static RedisBusMessage New(string subscriberName = null, bool durable = false,
            string topicName = null, string filter = null, bool needHeader = false, string transportName = null,
            ITransport transport = null, TopicInfo topic = null, bool async = true, bool caseSensitive = true,
            ESBIdentity userIdentity = null, bool reThrowException = false, Dictionary<string, string> headers = null) {
            var identity = userIdentity ?? ESBPrincipal.Current.ESBIdentity;
            var stack = new StackTrace(false);
            var commandName = stack.GetFrame(1).GetMethod().Name;
            return new RedisBusMessage()
            {
                SubscriberName = subscriberName,
                Durable = durable,
                Filter = filter,
                NeedHeader = needHeader,
                CommandName = commandName,
                Topic = topic,
                TopicName = topicName,
                Transport = transport,
                CaseSensitive = caseSensitive,
                TransportName = transportName,
                UserContext = new ESBUserContext()
                {
                    UserName = identity.Name,
                    Password = identity.Password
                },
                IsAsync = async,
                ReThrowException = reThrowException,
                Headers = headers
            };
        }
    }
}
